<?php

namespace Admin;

use AdminController;
use Config;
use DB;
use Input;
use Lang;
use Redirect;
use Sentry;
use Str;
use Validator;
use View;
use Jinput;
use Question;
use Request;

class QuestionsController extends AdminController {

    public function __construct() {
        $this->beforeFilter('auth');
    }

    public function getIndex() {
        $data['appends']['category'] = Jinput::get('category', -1);
        $myModel = new \MyModel();
        if ($data['appends']['category'] > -1)
            $arrParams['conditions'][] = array('field' => 'questions.category_id', 'opr' => '=', 'value' => $data['appends']['category']);
      
        $arrParams['left_joins'][] = array('table' => 'categories', 'pk' => 'categories.id', 'fk' => 'category_id');
        $arrParams['paginate']['appends'] = $data['appends'];
        $arrParams['paginate']['nums'] = 25;
        $arrParams['selects'] = array('questions.id', 'questions.title', 'questions.category_id', 'questions.is_hot', 'questions.created_at', 'categories.title as category_title');
        $data['questions'] = $myModel->listItems('Question', $arrParams);
        $data['categories'] = $this->dataCategories();

        return View::make('backend/questions/index', array('data' => $data));
    }

    /**
     *
     */
    public function getEdit($id) {
//         if (!Sentry::getUser()->hasAnyAccess(['question', 'question.edit']))
//            return View::make('backend/notallow');
        if (is_null($data['question'] = Question::find($id)))
            return Redirect::route('questions')->with('error', 'Nội dung không tồn lại');
        $data['tags'] = $data['question']->tags()->select('tags.*')->get();

        $myModel = new \MyModel();
        $data['categories'] = $this->dataCategories();
        return View::make('backend/questions/edit', array("data" => $data));
    }

    public function postEdit($id) {
        $data = Input::get('data');
        $rules = array('title' => 'required', 'content' => 'required|min:10');
        $validation = Validator::make($data['Question'], $rules);
        if ($validation->fails()) {
            return Redirect::back()->withInput()->withErrors($validation);
        }
        $mymodel = new \MyModel();
        $question = $mymodel->saveItem('Question', $data['Question'], array('return' => 'class'));
        if (isset($data['Tags']) && $data['Tags'] != '') {
            $tags = explode(',', $data['Tags']);
            foreach ($tags as $tag) {
                $tag_id = \Tag::Check($tag);
                $question->tags()->attach($tag_id);
            }
        }
        return Redirect::route('edit/question', $id)->with('success', 'Hợp âm đã được lưu');
    }

    public function getDelete($id) {
//        if (!Sentry::getUser()->hasAnyAccess(['question', 'question.delete']))
//            return View::make('backend/notallow');
        if (is_null($question = Question::find($id)))
            return Redirect::to('admin/question')->with('error', '');
        try {
            $question->delete();
            return Redirect::to('admin/questions');
        } catch (Exception $ex) {
            return Redirect::to('delete/question', $id)->with('error', $ex);
        }
    }

    public function checkHotQuestion() {
        if (Input::get('save_hot') != '') {
            $id_question = Input::get('save_hot');
            $question = \Question::find($id_question);
            $check = "";
            if ($question->is_hot == 0) {
                $question->is_hot = 1;
                $check = 'checked';
            } else
                $question->is_hot = 0;
            $question->save();
            $html = '<input id="check_hot' . $question->id . '" value ="' . $question->id . '" style="text-align: center" type="checkbox" ' . $check . ' class="checkbox-inline"/>';
            return $html;
        }
    }

//data category
    public function dataCategories() {

        $data = \Category::
                where('type', '=', 'lesson')
                ->lists('title', 'id');
        return $data;
    }

}
